package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.OmsBargainMapper;
import com.fc.mall.mapper.PmsProductBargainMapper;
import com.fc.mall.model.OmsBargain;
import com.fc.mall.model.OmsBargainExample;
import com.fc.mall.model.PmsProductBargain;
import com.fc.mall.portal.dao.BargainProductDao;
import com.fc.mall.portal.domain.BargainParam;
import com.fc.mall.portal.domain.BargainProduct;
import com.fc.mall.portal.service.PmsBargainService;
import com.fc.mall.portal.service.ProductService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.portal.vo.PmsProductVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by sunny on 2019/9/4.
 */

@Service
public class PmsBargainServiceImpl implements PmsBargainService {

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private ProductService productService;

    @Autowired
    private PmsProductBargainMapper productBargainMapper;

    @Autowired
    private BargainProductDao bargainProductDao;

    @Autowired
    private OmsBargainMapper omsBargainMapper;

    @Autowired
    private UmsMemberService memberService;

    @Override
    public BargainProduct getBargainInfo(Long id) {
        BargainProduct bargainProduct = new BargainProduct();
        PmsProductBargain productBargain = productBargainMapper.selectByPrimaryKey(id);
        PmsProductVO productVO = productService.getInfo(productBargain.getProductId());
        mapperFacade.map(productBargain, bargainProduct);
        mapperFacade.map(productVO, bargainProduct);

        OmsBargainExample omsBargainExample = new OmsBargainExample();
        omsBargainExample.createCriteria().andOwnerIdEqualTo(memberService.getCurrentMember().getId())
        .andExpiredTimeGreaterThan(new Date()).andIsAddorderEqualTo(0).andIsAddorderEqualTo(0);
        List<OmsBargain> omsBargainList = omsBargainMapper.selectByExample(omsBargainExample);

        if(null != omsBargainList && !omsBargainList.isEmpty())
        {
            bargainProduct.setOwnerBargainId(omsBargainList.get(0).getId());
        }
        else
        {
            bargainProduct.setOwnerBargainId(0L);
        }
        return bargainProduct;
    }

    @Override
    public List<BargainProduct> getList(BargainParam bargainParam, Integer pageNum, Integer pageSize) {
        return bargainProductDao.queryList(bargainParam, pageNum, pageSize);
    }



}
